package org.lk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lk.reggie.dto.SetmealDto;
import org.lk.reggie.entity.Dish;
import org.lk.reggie.entity.Setmeal;
import org.lk.reggie.entity.SetmealDish;
import org.lk.reggie.mapper.SetmealMapper;
import org.lk.reggie.service.SetmealDishService;
import org.lk.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService
{

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void mySave(SetmealDto setmealDto)
    {
        // 保存基本信息到套餐表
        this.save(setmealDto);

        List<SetmealDish> setmealDtoDishes = setmealDto.getSetmealDishes();

        for (SetmealDish dish:
             setmealDtoDishes)
        {
            dish.setSetmealId(setmealDto.getId());
        }

        setmealDishService.saveBatch(setmealDtoDishes);
    }

    @Override
    @Transactional
    public void myDelete(Long id) {

        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishQueryWrapper.eq(SetmealDish :: getSetmealId, id);
        setmealDishService.remove(setmealDishQueryWrapper);

        this.removeById(id);
    }


}
